package com.example.selfcontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.selfcontrol.config.Response;
import com.example.selfcontrol.entity.Article;
import com.example.selfcontrol.entity.StarOpt;
import com.example.selfcontrol.entity.resp.StarInfo;
import com.example.selfcontrol.mapper.ArticleMapper;
import com.example.selfcontrol.mapper.StarOptMapper;
import com.example.selfcontrol.service.StarOptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StarOptServiceImpl implements StarOptService {

    @Autowired
    private StarOptMapper starOptMapper ;

    @Override
    public Response ClickStar(int articleId, int userId) {
        System.out.println(articleId);
        System.out.println(userId);
        QueryWrapper<StarOpt> wrapper = new QueryWrapper<>() ;
        wrapper.eq("article_id",articleId).eq("star_user",userId) ;
        StarOpt find = starOptMapper.selectOne(wrapper) ;

        if(find != null) {
            // 存在记录就反转状态
            int newState = find.getState() ^ 1 ;
            find.setUpdateTime(new Date());
            find.setState(newState);
            starOptMapper.update(find,wrapper) ;
            return new Response(true,"200",newState) ;
        }else {
            StarOpt tmp = new StarOpt(0,articleId,1,userId,new Date() ,new Date()) ;
            starOptMapper.insert(tmp) ;
            return new Response(true,"200",1) ;
        }
    }
    @Override
    public StarInfo getStarInfo(int articleId, int userId) {
        QueryWrapper<StarOpt> wrapper = new QueryWrapper<>() ;
        wrapper.eq("article_id",articleId).eq("star_user",userId);
        StarOpt find = starOptMapper.selectOne(wrapper) ;

        wrapper.clear();
        wrapper.eq("article_id",articleId) ;
        int starNumber = starOptMapper.selectCount(wrapper);

        if(find != null)            return new StarInfo(true,starNumber) ;
        return new StarInfo(false,starNumber) ;
    }

    @Override
    public int CountStar(Long articleId) {
        QueryWrapper<StarOpt> wrapper = new QueryWrapper<>() ;
        wrapper.eq("state",1) ;
        
        wrapper.eq("article_id",articleId);
        return starOptMapper.selectCount(wrapper) ;
    }


    @Autowired
    private ArticleMapper articleMapper ;

    @Override
    public List<Article> UserStar(int userId, int pageNum, int pageSize) {
        QueryWrapper<StarOpt> wrapper = new QueryWrapper() ;
        wrapper.eq("state",1) ;
        wrapper.eq("star_user",userId) ;

        Page<StarOpt> page = new Page<>(pageNum,pageSize) ;
        List<StarOpt> starOpts = starOptMapper.selectPage(page,wrapper).getRecords() ;

        List<Article> result = new ArrayList<>() ;
        for(StarOpt x : starOpts) {
            int articleId = x.getArticleId() ;
            Article article = articleMapper.selectById(articleId) ;
            result.add(article) ;
        }

        return result ;
    }

    @Override
    public int personalStarCount(int userId) {
        QueryWrapper<StarOpt> wrapper = new QueryWrapper() ;
        wrapper.eq("star_user",userId) ;
        wrapper.eq("state",1) ;

        return starOptMapper.selectCount(wrapper) ;
    }

    @Override
    public boolean isUserStar(int userId , Long  articleId) {
        QueryWrapper<StarOpt> wrapper = new QueryWrapper<>() ;
        wrapper.eq("star_user",userId).eq("article_id",articleId).eq("state",1);
        StarOpt find = starOptMapper.selectOne(wrapper) ;

        if(find != null)    return true ;
        return false ;
    }
}

